﻿Imports iTunesLib

Public Class settings

    Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
    Dim WithEvents iTunesApp As iTunesLib.iTunesApp
    Dim mainLibrary As IITSource
    Dim LibPlaylists As IITPlaylistCollection
    Dim COMStateActive As Boolean = True


    Private Sub settings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Settings.hide_mouse = 1 Then
            checkHideMouse.Checked = True
        Else
            checkHideMouse.Checked = False
        End If

        txtHeight.Text = My.Settings.cover_height
        txtWidth.Text = My.Settings.cover_width

        showBgColour.BackColor = My.Settings.bg_colour
        PINnew.Text = My.Settings.pin

        sliderVolume.Value = Main.iTunesApp.SoundVolume

        load_playlists()

    End Sub

    Public Sub load_playlists()
        Dim Sources As IITSourceCollection
        Dim pList As IITSource
        Dim myPlaylist As iTunesLib.IITPlaylist
        Dim myUserPlayList As iTunesLib.IITUserPlaylist
        Dim Count As Integer = 1
        Dim selectedIndex As Integer
        
        Sources = Main.iTunesApp.Sources 'iTunesApp.Sources
        pList = Sources.Item(1)

        'add the iTunes DJ playlist as this is the default
        listPlaylists.Items.Add("iTunes DJ")

        

        Do While Count <= pList.Playlists.Count
            myPlaylist = pList.Playlists.Item(Count)
            '
            ' If the playlist is a user playlist we want it
            '
            If myPlaylist.Kind = ITPlaylistKind.ITPlaylistKindUser Then
                '
                ' Copy the playlist over to a userplaylist, dunno how or why this works
                ' but you get access to more information, like specialkind and parent
                '
                myUserPlayList = myPlaylist

                If myUserPlayList.Parent Is Nothing Then
                    '
                    ' No parent so add node to tree root if it is not a special kind but is a folder (ignore Video, Podcast etc...)
                    '
                    If myUserPlayList.SpecialKind = ITUserPlaylistSpecialKind.ITUserPlaylistSpecialKindNone _
                    Or myUserPlayList.SpecialKind = ITUserPlaylistSpecialKind.ITUserPlaylistSpecialKindFolder Then
                        listPlaylists.Items.Add(myPlaylist.Name)
                        If My.Settings.playlist = myPlaylist.Name Then
                            selectedIndex = Count
                        End If
                    End If

                End If

            End If

            Count += 1
        Loop

        If My.Settings.playlist = "iTunes DJ" Then
            listPlaylists.SelectedIndex = 0 'this will always be first
        Else
            listPlaylists.SelectedIndex = listPlaylists.FindString(My.Settings.playlist)
        End If

    End Sub


    



    Private Sub btnQuitJukebox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuitJukebox.Click
        If My.Settings.hide_mouse = 1 Then
            'blnShow = Not blnShow
            ShowCursor(True)
        End If
        iTunesApp = Nothing
        Application.Exit()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If checkHideMouse.Checked = True Then
            My.Settings.hide_mouse = 1
        Else
            My.Settings.hide_mouse = 0
        End If

        If My.Settings.cover_height <> txtHeight.Text Or My.Settings.cover_width <> txtWidth.Text Then
            'user must have changed, so force a refresh of cover info
            My.Settings.redraw_album_covers = 1
        End If

        My.Settings.cover_height = txtHeight.Text
        My.Settings.cover_width = txtWidth.Text
        My.Settings.bg_colour = showBgColour.BackColor
        'update the parent bg colour too
        Main.BackColor = showBgColour.BackColor

        If PINnew.Text <> "" Then
            My.Settings.pin = PINnew.Text
        End If

        If My.Settings.playlist = "" Then
            My.Settings.playlist = "iTunes DJ" 'just a temp reset in case I f**k up again!
        Else
            My.Settings.playlist = listPlaylists.GetItemText(listPlaylists.SelectedItem)
            Main.playListName = listPlaylists.GetItemText(listPlaylists.SelectedItem)
            Main.loadPlaylist_iTunesDJ() 'refresh the list
            Main.playFirstSongInPlaylist() 'force to play the next song in the list
        End If

        
        My.Settings.Save()
        Me.Close()

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Public Sub show_num_pad(ByVal field As TextBox)
        Dim dialog As New dialogNumPad

        dialog.NumberString = String.Empty

        If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            field.Text = dialog.NumberString
        Else
            ' User clicked the Cancel button
        End If
    End Sub


    Public Sub validate_field(ByVal field As TextBox)
        If field.Text > 500 Then
            MsgBox("The value cannot be greater than 500")
            field.Text = Nothing
            show_num_pad(txtWidth)
        End If
    End Sub

    Private Sub txtWidth_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWidth.Click
        show_num_pad(txtWidth)
    End Sub

    Private Sub txtWidth_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtWidth.Validating
        validate_field(txtWidth)
    End Sub

    Private Sub txtHeight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHeight.Click
        show_num_pad(txtHeight)
    End Sub

    Private Sub txtHeight_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtHeight.Validating
        validate_field(txtHeight)
    End Sub

    Private Sub PINnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PINnew.Click
        show_num_pad(PINnew)
    End Sub

    Private Sub PINnew_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PINnew.TextChanged

    End Sub


    Private Sub showBgColour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles showBgColour.Click
        Dim bgColour As New ColorDialog
        bgColour.Color = My.Settings.bg_colour
        If bgColour.ShowDialog() = DialogResult.OK Then
            showBgColour.BackColor = bgColour.Color
        End If
    End Sub


    Private Sub settingsTabControl_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles settingsTabControl.DrawItem

        'Firstly we'll define some parameters.
        Dim CurrentTab As TabPage = settingsTabControl.TabPages(e.Index)
        Dim ItemRect As Rectangle = settingsTabControl.GetTabRect(e.Index)
        Dim FillBrush As New SolidBrush(Color.Gainsboro)
        Dim TextBrush As New SolidBrush(Color.Black)
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center

        'If we are currently painting the Selected TabItem we'll 
        'change the brush colors and inflate the rectangle.
        If CBool(e.State And DrawItemState.Selected) Then
            FillBrush.Color = Color.WhiteSmoke
            TextBrush.Color = Color.Black
            ItemRect.Inflate(2, 2)
        End If

        'Set up rotation for left and right aligned tabs
        If settingsTabControl.Alignment = TabAlignment.Left Or settingsTabControl.Alignment = TabAlignment.Right Then
            Dim RotateAngle As Single = 90
            If settingsTabControl.Alignment = TabAlignment.Left Then RotateAngle = 270
            Dim cp As New PointF(ItemRect.Left + (ItemRect.Width \ 2), ItemRect.Top + (ItemRect.Height \ 2))
            e.Graphics.TranslateTransform(cp.X, cp.Y)
            e.Graphics.RotateTransform(RotateAngle)
            ItemRect = New Rectangle(-(ItemRect.Height \ 2), -(ItemRect.Width \ 2), ItemRect.Height, ItemRect.Width)
        End If

        'Next we'll paint the TabItem with our Fill Brush
        e.Graphics.FillRectangle(FillBrush, ItemRect)

        'Now draw the text.
        e.Graphics.DrawString(CurrentTab.Text, e.Font, TextBrush, RectangleF.op_Implicit(ItemRect), sf)

        'Reset any Graphics rotation
        e.Graphics.ResetTransform()

        'Finally, we should Dispose of our brushes.
        FillBrush.Dispose()
        TextBrush.Dispose()

    End Sub

#Region "player controls"

    Public Sub btnNextTrack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextTrack.Click
        Main.iTunesApp.NextTrack()
    End Sub
    'Public Sub btnPreviousTrack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Main.iTunesApp.PreviousTrack()
    'End Sub

    Public Sub sliderVolume_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sliderVolume.Scroll
        Main.iTunesApp.SoundVolume = sliderVolume.Value
    End Sub

#End Region


    Private Sub TabPage3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage3.Click

    End Sub

    Private Sub size50_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles size50.Click
        txtHeight.Text = 50
        txtWidth.Text = 50
    End Sub

    Private Sub size80_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles size80.Click
        txtHeight.Text = 80
        txtWidth.Text = 80
    End Sub

    Private Sub size100_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles size100.Click
        txtHeight.Text = 100
        txtWidth.Text = 100
    End Sub

    Private Sub size120_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles size120.Click
        txtHeight.Text = 120
        txtWidth.Text = 120
    End Sub

    Private Sub size150_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles size150.Click
        txtHeight.Text = 150
        txtWidth.Text = 150
    End Sub

    Private Sub size200_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles size200.Click
        txtHeight.Text = 200
        txtWidth.Text = 200
    End Sub
End Class